package com.lcz.host;

import android.Manifest;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.ryg.dynamicload.internal.DLIntent;
import com.ryg.dynamicload.internal.DLPluginManager;
import com.ryg.utils.DLUtils;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ArrayList<PluginItem> mPluginItems = new ArrayList<PluginItem>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
        if (lackPermissions(permissions)) {
            getPermission(permissions);
        }
        findViewById(R.id.btnLoadApk).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadApk();
            }
        });
        findViewById(R.id.btnCallPluginApk).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callPluginApk();
            }
        });
    }
    public boolean lackPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return false;
        }
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }
    private void getPermission(String[] permissions) {
        Toast.makeText(this,"宿主开始请求权限", Toast.LENGTH_SHORT).show();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.requestPermissions(permissions, 1000);
        }
    }
    private void callPluginApk() {
        PluginItem item = mPluginItems.get(0);
        DLPluginManager pluginManager = DLPluginManager.getInstance(this);
        pluginManager.startPluginActivity(this, new DLIntent(item.packageInfo.packageName, item.launcherActivityName));
    }

    private void loadApk() {
//        String pluginFolder = Environment.getExternalStorageDirectory() + "/DynamicLoadHost";
        String pluginFolder = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/DynamicLoadHost";//   /sdcard/Android/data/com.lcz.host/files/Download/DynamicLoadHost/app-release.apk
        File file = new File(pluginFolder);
        if (!file.exists()) {
            file.mkdirs();
        }
        File[] plugins = file.listFiles();
        if (plugins == null || plugins.length == 0) {
            Toast.makeText(this, "无插件", Toast.LENGTH_SHORT).show();
            return;
        }

        for (File plugin : plugins) {
            if (plugin.getName().endsWith(".apk")) {
                PluginItem item = new PluginItem();
                item.pluginPath = plugin.getAbsolutePath();
                item.packageInfo = DLUtils.getPackageInfo(this, item.pluginPath);
                if (item.packageInfo.activities != null && item.packageInfo.activities.length > 0) {
                    item.launcherActivityName = item.packageInfo.activities[0].name;
                }
                if (item.packageInfo.services != null && item.packageInfo.services.length > 0) {
                    item.launcherServiceName = item.packageInfo.services[0].name;
                }
                mPluginItems.add(item);
                DLPluginManager.getInstance(this).loadApk(item.pluginPath);
            }

        }
        Toast.makeText(this, "插件加载成功", Toast.LENGTH_SHORT).show();

    }

    public static class PluginItem {
        public PackageInfo packageInfo;
        public String pluginPath;
        public String launcherActivityName;
        public String launcherServiceName;

        public PluginItem() {
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}